//
//  141. 环形链表.swift
//  Top600
//
//  Created by rjb on 2021/8/8.
//

import Foundation
class Solution141 {
    // 判定一个链表有环。采用快慢指针就可以了
    // 测试用例： 至于一个节点时
    func hasCycle(_ head: ListNode?) -> Bool {
        var slow = head
        var fast = head
        while fast != nil {
            slow = slow?.next
            fast = fast?.next?.next
            if slow === fast && fast != nil{
                return true
            }
        }
        return false
    }
    static func test() {
        let solution = Solution141()
        let node3 = ListNode(3)
        let node2 = ListNode(2)
        let node0 = ListNode(0)
        let node4 = ListNode(-4)
        node3.next = node2
        node2.next = node0
        node0.next = node4
        node4.next = node2
        
        let result = solution.hasCycle(node3)
        print(result)
    }
}
